package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.pojo.Question;
import com.tanhua.server.service.TanHuaService;
import com.tanhua.server.vo.NearUserVo;
import com.tanhua.server.vo.TodayBest;
import com.tanhua.server.vo.UserInformation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/tanhua")
public class TanHuaController {

    @Autowired
    private TanHuaService tanHuaService;

    @GetMapping("/{id}/personalInfo")
    public ResponseEntity<UserInformation> queryList(@PathVariable("id") Long userId) {
        try {
            UserInformation userInformation = this.tanHuaService.queryUserInfo(userId);
            if (ObjectUtil.isNotEmpty(userInformation)) {
                return ResponseEntity.ok(userInformation);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查看陌生人问题
     *
     * @param userId
     * @return
     */
    @GetMapping("/strangerQuestions")
    public ResponseEntity<String> queryQuestion(@RequestParam("userId") Long userId) {
        try {
            String txt = this.tanHuaService.queryQuestion(userId);
            if (ObjectUtil.isNotEmpty(txt)) {
                return ResponseEntity.ok(txt);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("/strangerQuestions")
    public ResponseEntity<Boolean> replyQuestion(@RequestBody Map<String, Object> param){
        try {
            Long userId = Long.valueOf(param.get("userId").toString());
            String reply = param.get("reply").toString();
            if (!ObjectUtil.isAllNotEmpty()){
                log.info("用户未回复消息！");
                return ResponseEntity.ok(null);
            }
            Boolean result = this.tanHuaService.replyQuestion(userId, reply);
            return ResponseEntity.ok(result);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 搜索附近的人
     *
     * @param gender
     * @param distance  距离，单位：米
     * @return
     */
    @GetMapping("search")
    public ResponseEntity<List<NearUserVo>> queryNearUser(@RequestParam(value = "gender",required = false) String gender,
                                                          @RequestParam(value = "distance", defaultValue = "2000") String distance){

        try {
            List<NearUserVo> nearUserVoList = this.tanHuaService.queryNearUser(gender, distance);
            return ResponseEntity.ok(nearUserVoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 探花
     *
     * @return
     */
    @GetMapping("/cards")
    public ResponseEntity<List<UserInformation>> queryCardsList() {
        try {
            List<UserInformation> list = this.tanHuaService.queryCardsList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 左滑右滑-喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/love")
    public ResponseEntity<Void> likeUser(@PathVariable("id") Long likeUserId) {
        try {
            Boolean flag = this.tanHuaService.likeUser(likeUserId);
            if (flag){
                return ResponseEntity.ok(null);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 左滑右滑-不喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/unlove")
    public ResponseEntity<Void> notLikeUser(@PathVariable("id") Long likeUserId) {
        try {
            Boolean flag = this.tanHuaService.notLikeUser(likeUserId);
            if (flag){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
